﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace LiveResults
{
    public partial class MainApp : Form
    {
        public MainApp()
        {
            InitializeComponent();

            loadRecentProjects();
        }

        private void loadRecentProjects()
        {
            string path = GetPath();
            if (System.IO.File.Exists(path))
            {
                try
                {
                    BinaryFormatter serializer = new BinaryFormatter();
                    FileStream fs = File.OpenRead(path);
                    LastProjects = (List<BroadCastProject>)serializer.Deserialize(fs);
                    fs.Close();
                }
                catch
                {
                }
            }
        }

        public static void SaveRecentProjects()
        {
            string path = GetPath();
            if (!System.IO.Directory.Exists(Path.GetDirectoryName(path)))
            {
                System.IO.Directory.CreateDirectory(Path.GetDirectoryName(path));
            }
            BinaryFormatter s = new BinaryFormatter();
            FileStream fs = File.Create(path);
            s.Serialize(fs, LastProjects);
            fs.Close();
        }

        private static string GetPath()
        {
            string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "LiveResults_2_0", "projects.dat");
            return path;
        }

        public static BroadCastProject CurrentProject = null;

        public static List<BroadCastProject> LastProjects = new List<BroadCastProject>();

        private void MainApp_Shown(object sender, EventArgs e)
        {
            FrmSelProject frm = new FrmSelProject();
            bool newChoice = false;
            do
            {
                frm.StartPosition = FormStartPosition.CenterParent;
                if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    if (frm.IsCreateNew)
                    {
                        FrmNewProject p = new FrmNewProject();
                        if (p.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                        {
                            newChoice = false;
                        }
                        else
                        {
                            newChoice = true;
                        }
                    }
                    else
                    {
                        newChoice = false;
                        CurrentProject = frm.ExistingProjectToOpen;
                        LastProjects.Remove(CurrentProject);
                        LastProjects.Insert(0, CurrentProject);
                        SaveRecentProjects();
                    }
                    if (!newChoice)
                        loadProject();
                }
                else
                {
                    this.Close();
                    newChoice = false;
                }
            }
            while (newChoice);

        }

        FrmStatusWin m_StatusWin = null;
        private void loadProject()
        {
            m_StatusWin = new FrmStatusWin();
            m_StatusWin.MdiParent = this;
            m_StatusWin.Dock = DockStyle.Bottom;
            m_StatusWin.Height = 150;
            m_StatusWin.Show();


            logit("Fetching existing data from server");
            logit("Done");
            if (CurrentProject != null)
            {
                FrmMonitor frm = null;
                if (CurrentProject.EventSoftwareType is EventSoftwareOE)
                {
                    frm = new FrmOEMonitor(CurrentProject.EventSoftwareType as EventSoftwareOE, new logDelegate(logit));
                }
                else if (CurrentProject.EventSoftwareType is EventSoftwareOLA)
                {
                    frm = new FrmOlaMonitor(CurrentProject.EventSoftwareType as EventSoftwareOLA, new logDelegate(logit));
                }

                if (frm != null)
                {
                    frm.MdiParent = this;
                    frm.Dock = DockStyle.Fill;
                    frm.Show();
                }
            }
        }

        public delegate void logDelegate(string msg);

        void logit(string msg)
        {
            if (m_StatusWin != null)
            {
                m_StatusWin.logit(msg);
            }
        }

        private void statusToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (m_StatusWin != null && !m_StatusWin.IsDisposed)
            {
                m_StatusWin.Show();
            }
        }
    }
}
